Skip to content

TIP

本文以 MacOS 12.1、Laravel 10.x 框架为例

安装扩展包

根目录下:

bash
composer require --dev brainmaestro/composer-git-hooks:v3.0.0-alpha.1

如下图:

file

修改配置

修改 composer.json 文件

  1. 添加 scripts 选项

    json
    {
      "scripts": {
        "post-update-cmd": [
          "@php artisan vendor:publish --tag=laravel-assets --ansi --force",
          "cghooks update"
        ],
        "post-merge": "composer install", 
        "post-install-cmd": [
    
          "cghooks add --ignore-lock", 
          "cghooks update"
        ], 
        "cghooks": "vendor/bin/cghooks"
      }
    }
  2. extra 选项中添加 hooks 选项

    json
    {
      "extra": {
        "laravel": {
          "dont-discover": []
        },
        "hooks": {
    
          "config": {
    
            "stop-on-failure": [
    
              "pre-commit", 
              "pre-push"
            ] 
          }, 
          "pre-commit": [
    
            "echo Running pre-commit hooks...", 
            "./vendor/bin/pint", 
            "echo Running tests...", 
            "php artisan test"
          ], 
          "commit-msg": "grep -qE '^(feat|fix|chore|docs|style|refactor|perf|test|build|ci|revert)\\([^)]+\\):\\s.+\\s?(?:\\(#\\d+\\))?' $1 || (echo 'Commit message must follow Conventional Commits specification!' && exit 1)", 
          "pre-push": [
    
            "echo Running pre-push hooks...", 
            "./vendor/bin/pint --test", 
            "echo Running tests...", 
            "php artisan test"
          ], 
          "post-merge": "composer install"
        } 
      }
    }

添加 Git Hooks

根目录下:

bash
composer cghooks add --force-win

TIP

更多信息可参考:brainmaestro/composer-git-hooks

Released under the MIT License.